package com.micmac.customer.view.mediator
{
	import com.micmac.customer.events.CustomerEvent;
	import com.micmac.customer.interfaces.ICustomerService;
	import com.micmac.customer.model.SalesProcessModel;
	import com.micmac.customer.view.SalesProcessView;
	import com.micmac.customer.vo.SalesProcessVO;
	import com.micmac.customer.vo.SalesStageVO;
	import com.micmac.customer.vo.SalesStepVO;
	
	import flash.events.Event;
	
	import org.robotlegs.utilities.modular.mvcs.ModuleMediator;

	public class SalesProcessViewMediator extends ModuleMediator
	{
		[Inject]
		public var salesProcessView:SalesProcessView;
		[Inject]
		public var service:ICustomerService;
		[Inject]
		public var _dataModel:SalesProcessModel;
		
		private var _salesProcessIndex:int = 0;
		private var _salesStageIndex:int = 0;
		private var _salesStepIndex:int = 0;
		
		override public function onRegister():void
		{
			addViewListener(SalesProcessView.SAVE_SALES_PROCESS,onSaveSalesProcess);
			addViewListener(SalesProcessView.SAVE_SALES_STAGE,onSaveSalesStage);
			addViewListener(SalesProcessView.SAVE_SALES_STEP,onSaveSalesStep);
			
			addViewListener(SalesProcessView.SALES_PROCESS_PREVIOUS,onSalesProcessPrevious);
			addViewListener(SalesProcessView.SALES_STAGE_PREVIOUS,onSalesStagePrevious);
			addViewListener(SalesProcessView.SALES_STEP_PREVIOUS,onSalesStepPrevious);
			
			addViewListener(SalesProcessView.SALES_PROCESS_NEXT,onSalesProcessNext);
			addViewListener(SalesProcessView.SALES_STAGE_NEXT,onSalesStageNext);
			addViewListener(SalesProcessView.SALES_STEP_NEXT,onSalesStepNext);
			
			addViewListener(SalesProcessView.STACK_CHANGED,onStackChanged);
			
			eventMap.mapListener(eventDispatcher,CustomerEvent.SALES_PROCESS_SAVED,onSavedSalesProcess);
			eventMap.mapListener(eventDispatcher,CustomerEvent.SALES_STAGE_SAVED,onSavedSalesStage);
			eventMap.mapListener(eventDispatcher,CustomerEvent.SALES_STEP_SAVED,onSavedSalesStep);
		}
		
		private function onSaveSalesProcess(event:Event):void
		{
			var salesProcessVO:SalesProcessVO = new SalesProcessVO();
			salesProcessVO.processName = salesProcessView.salesProcessNameText.text;
			salesProcessVO.description = salesProcessView.salesProcessDescText.text;
			salesProcessVO.status = salesProcessView.salesProcessStatusCombo.textInput.text;
			service.saveSalesProsess(salesProcessVO);
		}
		
		private function onSaveSalesStage(event:Event):void
		{
			var salesStageVO:SalesStageVO = new SalesStageVO();
			salesStageVO.name = salesProcessView.salesStageName.text;
			salesStageVO.description = salesProcessView.salesStageDesc.text;
			salesStageVO.process = salesProcessView.salesStageProcess.textInput.text;
			salesStageVO.parentStage = salesProcessView.salesStageParentStage.textInput.text;
			salesStageVO.sequence =  salesProcessView.salesStageSequenceText.text;
			if(salesProcessView.salesStageActiveCheck.selected)
			{
				salesStageVO.isAcive = true;
			}else
			{
				salesStageVO.isAcive = false;
			}
			salesStageVO.weight = Number(salesProcessView.salesStageNumericStepper.textDisplay.text);
			service.saveSalesStage(salesStageVO);
		}
		
		private function onSaveSalesStep(event:Event):void
		{
			var salesStepVo:SalesStepVO = new SalesStepVO();
			salesStepVo.name = salesProcessView.salesStagenameText.text;
			salesStepVo.description = salesProcessView.salesStepDesc.text;
			service.saveSalesStep(salesStepVo);
		}
		
		private function onSavedSalesProcess(event:CustomerEvent = null):void
		{
			if(_dataModel.salesProcessList.length != 1 && _dataModel.salesProcessList.length != 0)
			{
				salesProcessView.prevBtn.enabled = true;
			}else
			{
				salesProcessView.prevBtn.enabled = false;
			}
			salesProcessView.addSalesBtn.enabled = true;
			salesProcessView.saveBtn.enabled = false;
			salesProcessView.nextBtn.enabled = false;
			_salesProcessIndex = _dataModel.salesProcessList.length - 1;
			salesProcessView.addSalesBtn.label = '+process';
		}
		
		private function onSavedSalesStage(event:CustomerEvent = null):void
		{
			if(_dataModel.salesStageList.length != 1 && _dataModel.salesStageList.length != 0)
			{
				salesProcessView.prevBtn.enabled = true;
			}else
			{
				salesProcessView.prevBtn.enabled = false;
			}
			salesProcessView.addSalesBtn.enabled = true;
			salesProcessView.saveBtn.enabled = false;
			salesProcessView.nextBtn.enabled = false;
			_salesStageIndex = _dataModel.salesStageList.length - 1;
			salesProcessView.addSalesBtn.label = '+stage';
		}
		
		private function onSavedSalesStep(event:CustomerEvent = null):void
		{
			if(_dataModel.salesStepList.length != 1 && _dataModel.salesStepList.length != 0)
			{
				salesProcessView.prevBtn.enabled = true;
			}else
			{
				salesProcessView.prevBtn.enabled = false;
			}
			salesProcessView.addSalesBtn.enabled = true;
			salesProcessView.saveBtn.enabled = false;
			salesProcessView.nextBtn.enabled = false;
			_salesStepIndex = _dataModel.salesStepList.length - 1;
			salesProcessView.addSalesBtn.label = '+step';
		}
		
		private function onSalesProcessPrevious(event:Event):void
		{
			_salesProcessIndex--;
			salesProcessView.nextBtn.enabled = true;
			salesProcessView.saveBtn.enabled = false;
			
			salesProcessView.salesProcessNameText.text = _dataModel.salesProcessList[_salesProcessIndex].processName;
			salesProcessView.salesProcessDescText.text = _dataModel.salesProcessList[_salesProcessIndex].description;
			salesProcessView.salesProcessStatusCombo.textInput.text = _dataModel.salesProcessList[_salesProcessIndex].status;
			
			if(_salesProcessIndex == 0)
			{
				salesProcessView.prevBtn.enabled = false;
			}
		}
		
		private function onSalesStagePrevious(event:Event):void
		{
			_salesStageIndex--;
			salesProcessView.nextBtn.enabled = true;
			salesProcessView.saveBtn.enabled = false;
			
			salesProcessView.salesStageName.text = _dataModel.salesStageList[_salesStageIndex].name;
			salesProcessView.salesStageDesc.text = _dataModel.salesStageList[_salesStageIndex].description;
			salesProcessView.salesStageProcess.textInput.text = _dataModel.salesStageList[_salesStageIndex].process;
			salesProcessView.salesStageParentStage.textInput.text = _dataModel.salesStageList[_salesStageIndex].parentStage;
			salesProcessView.salesStageSequenceText.text = _dataModel.salesStageList[_salesStageIndex].sequence;
			if(_dataModel.salesStageList[_salesStageIndex].isAcive == true)
			{
				salesProcessView.salesStageActiveCheck.selected = true;
			}else
			{
				salesProcessView.salesStageActiveCheck.selected = false;
			}
			salesProcessView.salesStageNumericStepper.textDisplay.text = _dataModel.salesStageList[_salesStageIndex].weight;
			if(_salesStageIndex == 0)
			{
				salesProcessView.prevBtn.enabled = false;
			}
		}
		
		private function onSalesStepPrevious(event:Event):void
		{
			_salesStepIndex--;
			salesProcessView.nextBtn.enabled = true;
			salesProcessView.saveBtn.enabled = false;
			
			salesProcessView.salesStagenameText.text = _dataModel.salesStepList[_salesStepIndex].name;
			salesProcessView.salesStepDesc.text = _dataModel.salesStepList[_salesStepIndex].description;
			
			if(_salesStepIndex == 0)
			{
				salesProcessView.prevBtn.enabled = false;
			}
		}
		
		private function onSalesProcessNext(event:Event):void
		{
			_salesProcessIndex++;
			salesProcessView.saveBtn.enabled = false;
			salesProcessView.prevBtn.enabled = true;
			
			salesProcessView.salesProcessNameText.text = _dataModel.salesProcessList[_salesProcessIndex].processName;
			salesProcessView.salesProcessDescText.text = _dataModel.salesProcessList[_salesProcessIndex].description;
			salesProcessView.salesProcessStatusCombo.textInput.text = _dataModel.salesProcessList[_salesProcessIndex].status;
			if(_salesProcessIndex == _dataModel.salesProcessList.length - 1)
			{
				salesProcessView.nextBtn.enabled = false;
			}
		}
		
		private function onSalesStageNext(event:Event):void
		{
			_salesStageIndex++;
			salesProcessView.saveBtn.enabled = false;
			salesProcessView.prevBtn.enabled = true;
			
			salesProcessView.salesStageName.text = _dataModel.salesStageList[_salesStageIndex].name;
			salesProcessView.salesStageDesc.text = _dataModel.salesStageList[_salesStageIndex].description;
			salesProcessView.salesStageProcess.textInput.text = _dataModel.salesStageList[_salesStageIndex].process;
			salesProcessView.salesStageParentStage.textInput.text = _dataModel.salesStageList[_salesStageIndex].parentStage;
			salesProcessView.salesStageSequenceText.text = _dataModel.salesStageList[_salesStageIndex].sequence;
			if(_dataModel.salesStageList[_salesStageIndex].isAcive == true)
			{
				salesProcessView.salesStageActiveCheck.selected = true;
			}else
			{
				salesProcessView.salesStageActiveCheck.selected = false;
			}
			salesProcessView.salesStageNumericStepper.textDisplay.text = _dataModel.salesStageList[_salesStageIndex].weight;
			
			if(_salesStageIndex == _dataModel.salesStageList.length - 1)
			{
				salesProcessView.nextBtn.enabled = false;
			}
		}
		
		private function onSalesStepNext(event:Event):void
		{
			_salesStepIndex++;
			salesProcessView.saveBtn.enabled = false;
			salesProcessView.prevBtn.enabled = true;
			
			salesProcessView.salesStagenameText.text = _dataModel.salesStepList[_salesStepIndex].name;
			salesProcessView.salesStepDesc.text = _dataModel.salesStepList[_salesStepIndex].description;
			
			if(_salesStepIndex == _dataModel.salesStepList.length - 1)
			{
				salesProcessView.nextBtn.enabled = false;
			}
		}
		
		private function onStackChanged(event:Event):void
		{
			switch(salesProcessView.vsSalesProcess.selectedIndex)
			{
				case 0:
					onSavedSalesProcess();
					salesProcessView.saveBtn.enabled = true;
					salesProcessView.addSalesBtn.enabled = false;
					break;
				case 1:
					onSavedSalesStage();
					salesProcessView.saveBtn.enabled = true;
					salesProcessView.addSalesBtn.enabled = false;
					break;
				case 2:
					onSavedSalesStep()
					salesProcessView.saveBtn.enabled = true;
					salesProcessView.addSalesBtn.enabled = false;
					break;
			}
		}
	}
}